<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>停车场设置</title>
		<link rel="stylesheet" type="text/css" href="../jquery-easyui-1.5.2/themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../jquery-easyui-1.5.2/themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../jquery-easyui-1.5.2/demo.css">
		<link rel="stylesheet" type="text/css" href="../style/syExtIcon.css">

		<script type="text/javascript" src="../jquery-easyui-1.5.2/jquery.min.js"></script>
		<script type="text/javascript" src="../jquery-easyui-1.5.2/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../js/syExtEasyUI.js"></script>
		<script type="text/javascript" src="../js/syExtHighcharts.js"></script>
		<script type="text/javascript" src="../js/syExtJavascript.js"></script>
		<script type="text/javascript" src="../js/syExtJquery.js"></script>
		<script type="text/javascript" src="../js/carpark.js"></script>
		<script type="text/javascript" src="carparkManage.js"></script>
		<script type="text/javascript">
			var grid;
			var carpark;
			var mainTree;
			var carparkData;
			var fixCharge;
			var fixChargeData;
			var tempChargeData;
			$(function() {
				loadTree();
				loadGrid('0');
			});

			function loadTree() {
				mainTree = $("#carparkTree").tree({
					url: '/device/getAllLoraBaseStation',
					method: 'post',
					animate: true,
					onClick: function(node) {
						if(node.attributes == null) {
							return;
						}
						carpark = node;
						loadGrid(node.attributes.id);
					}
				});
			}

			function loadGrid(cid) {
				grid = $('#grid').mygrid({
					title: '',
					url: '/carpark/listCharges?id=' + cid,
					pagination: false,
					frozenColumns: [
						[{
							width: '100',
							title: '基站编号',
							field: 'code',
							sortable: false
						}]
					],
					columns: [
						[{
							width: '100',
							title: '基站名称',
							field: 'name',
							sortable: false
						}, {
							width: '100',
							title: '基站通道',
							field: 'type',
							sortable: false
						}, {
							width: '150',
							title: '通道名称',
							field: 'carType',
							sortable: false
						}, {
							width: '80',
							title: '码率',
							field: 'holidayType',

						}, {
							width: '80',
							title: '频率',
							field: 'useType',

						}, {
							width: '80',
							title: '功耗',
							field: 'useType',

						}, {
							width: '80',
							title: '扩散因子',
							field: 'useType',

						}, {
							width: '80',
							title: '宽带',
							field: 'useType',
						}]
					],
					toolbar: '#toolbar',
					onClickRow: function(rowIndex, rowData) {
						fixCharge = rowData;
					}
				});
			}

			function addMainCarpark(title, data) {
				carparkData = data;
				var stationData={
					uuid:"",
					identifier:"",
					name:""
				};
				if(data!=null){
					stationData=data;
				}
				$("#addStationForm").form("load",stationData)
				var dialog=$("#addStationDialog").dialog({
					title: title == null ? "添加基站" : title,
					width: 300,
					height: 140,
					buttons: [{
						text: '保存',
						handler: function() {
							saveStation(dialog);
						}
					}, {
						text: '关闭',
						handler: function() {
							dialog.dialog("close")
						}
					}],
				});
			}
			function saveStation(dialog){
				if(!$("#addStationForm").form("validate")){
					return;
				}
				$.post("/device/addLoraBaseStation",$("#addStationForm").serialize(),function(ret){
					sy.info(ret.msg);
					if(ret.success){
						dialog.dialog("close")
					}
					
				})
			}
			
			function editCarpark() {
				if(carpark == null) {
					return;
				}
				var data = carpark.attributes;
				if(data.parentId == null) {
					addMainCarpark("修改停车场", carpark);
				} else {
					addChildCarpark("修改子停车场", carpark, data.parentId);
				}

			}

			function deleteCarpark() {
				$.messager.confirm("确认", "确认删除停车场[" + carpark.text + "]吗？", function(r) {
					if(r) {
						var txt = "/carpark/deleteCarpark";
						$.post(txt, {
							id: carpark.attributes.id
						}, function(data) {
							if(!data.success) {
								$.messager.alert("提示", data.msg, "error");
							} else {
								$.messager.alert("提示", data.msg, "info");
								reloadTree();
							}
						});
					}
				});
			}

			function reloadTree() {
				mainTree.tree("reload");
				carpark = null;
			}

			function addTempCharge(title, data, pid) {
				if(carpark == null && title == null) {
					return;
				}
				tempChargeData = data;
				var parentId = pid != null ? pid : carpark.attributes.id;
				var dialog = sy.modalDialog({
					title: title == null ? "添加临时收费" : title,
					width: 800,
					height: 610,
					url: "addTempCharge.html",
					buttons: [{
						text: '保存',
						handler: function() {
							dialog.find('iframe').get(0).contentWindow.submitNow(parentId, dialog, mainTree, $);
						}
					}, {
						text: '关闭',
						handler: function() {
							dialog.dialog("close")
						}
					}],
				});
			}

			function addFixCharge(title, data, pid) {
				if(carpark == null && title == null) {
					return;
				}
				fixChargeData = data;
				var parentId = pid != null ? pid : carpark.attributes.id;
				var dialog = sy.modalDialog({
					title: title == null ? "添加固定收费" : title,
					width: 400,
					height: 360,
					url: "addFixCharge.html",
					buttons: [{
						text: '保存',
						handler: function() {
							dialog.find('iframe').get(0).contentWindow.submitNow(parentId, dialog, mainTree, $);
						}
					}, {
						text: '关闭',
						handler: function() {
							dialog.dialog("close")
						}
					}],
				});

			}

			function editCharge() {
				if(fixCharge == null) {
					return;
				}
				$.post("/carpark/getCharge", fixCharge, function(d) {
					if(d == null||d.id==null) {
						sy.info("收费不存在");
						return;
					}
					if(fixCharge.type == "临时收费") {
						tempChargeData=d;
						addTempCharge("修改临时收费",d,d.carparkId)
					} else {
						addFixCharge("修改固定收费", d, d.carparkId);
					}
				});
			}

			function deleteCharge() {
				var row = grid.datagrid('getSelected');
				if(row == null) {
					return;
				}
				$.messager.confirm("确认", "确认删除停车场收费[" + row.code + "-" + row.name + "]吗？", function(r) {
					if(r) {
						$.post("/carpark/deleteCharge", row, function(data) {
							sy.info(data.msg);
							if(data.success) {
								loadDataGrid();
							}
						});
					}
				});
			}

			function enableCharge(isUse) {
				var row = grid.datagrid('getSelected');
				if(carpark == null || row == null) {
					return;
				}
				if(row.type != "临时收费") {
					return;
				}
				if(isUse && row.useType == '已启用') {
					return;
				}
				if(!isUse && row.useType == '未启用') {
					return;
				}
				$.post("/carpark/enableCharge", {
					id: row.id,
					code: row.code,
					isUse: isUse
				}, function(data) {
					if(data.success) {
						loadGrid(carpark.attributes.id);
					} else {
						sy.info(data.msg);
					}
				});
			}
			
function loadDataGrid(){
	grid.datagrid("clearSelections");
	grid.datagrid("load",{});
}

		</script>
	</head>

	<body class="easyui-layout">
		<div data-options="region:'west',split:true" title="基站列表" style="width: 300px; padding: 10px;">
			<ul id="carparkTree"></ul>
		</div>
		<div data-options="region:'center'" style="overflow: hidden;">
			<table id="grid" data-options="fit:true,border:false"></table>
		</div>
		<div id="toolbar" style="display: none;">
			<table>
				<tr>
					<td>
						<table style="display: inherit;" id="carparkManageTool">
							<tr>
								<td>
									<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-note_add',plain:true" onclick="addMainCarpark();">添加基站</a>
								</td>
								<td>
									<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-note_add',plain:true" onclick="editCarpark();">修改基站</a>
								</td>
								<td>
									<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-note_add',plain:true" onclick="deleteCarpark();">删除基站</a>
								</td>
								<td>
									<div class="datagrid-btn-separator"></div>
								</td>
								<td>
									<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-note_add',plain:true" onclick="addTempCharge();">添加通道</a>
								</td>
								<td>
									<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-note_add',plain:true" onclick="editCharge();">修改通道</a>
								</td>
								<td>
									<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-note_add',plain:true" onclick="deleteCharge();">删除通道</a>
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
		</div>
		<div id="addStationDialog" style="display: none;">
			<form id="addStationForm" method="post">
					<table class="table" style="width: 230px;margin: auto;">
						<tr style="display: none;">
							<th>基站id</th>
							<td><input name="uuid" value="" /></td>
						</tr>
						<tr>
							<th>基站编码</th>
							<td><input name="identifier" value="" style="width: 160px;" class="easyui-textbox" data-options="required:true" /></td>
						</tr>
						<tr>
							<th>基站名称</th>
							<td><input name="name" value="" style="width: 160px;" class="easyui-textbox" data-options="required:true" /></td>
						</tr>
					</table>
			</form>
		</div>
	</body>

</html>